package com.yanqu.road.server.cmd.activity.entername;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross3.activity.EnterNameProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.entername.EnterNameActivityConfig;
import com.yanqu.road.server.manger.activity.entername.EnterNameActivityMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Random;

@Cmd(code = Protocol.S_ENTER_NAME_GET_DISPERSE_GOLD_FROM_CHANNEL, desc = "领取散财")
public class EnterNameGetDisperseGoldRewardFromChannelCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        EnterNameProto.GetEnterNameDisperseGoldRewardReqMsg reqMsg = EnterNameProto.GetEnterNameDisperseGoldRewardReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        EnterNameActivityConfig config = EnterNameActivityMgr.getActivityConfig(activityId);
        if(config != null && ActivityMgr.activityInRewardPeriod(config.getActivityInfo())){
            String reward = config.getDisperseGoldReward();
            if(!StringUtils.isNullOrEmpty(reward)){
                List<Integer> paramList = StringUtils.stringToIntegerList(reward, "\\|");
                if(paramList.size() == 4){
                    Random random = new Random();
                    int second = paramList.get(0) + random.nextInt(paramList.get(1) - paramList.get(0));
                    BigInteger silver = null;
                    if(player != null){
                        silver = new BigDecimal(player.getUserInfo().getEarnSpeed()).multiply(BigDecimal.valueOf(second)).toBigInteger();
                    }else {
                        UserInfo userInfo = UserMgr.getUserInfo(packet.getPlayerId());
                        if(userInfo != null) {
                            silver = new BigDecimal(userInfo.getEarnSpeed()).multiply(BigDecimal.valueOf(second)).toBigInteger();
                        }
                    }
                    int ingots =  paramList.get(2) + random.nextInt(paramList.get(3) - paramList.get(2));
                    Property property = new Property();
                    property.addProperty(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(ingots));
                    property.addProperty(GameConfig.GAME_MONEY_SILVER, silver);
                    if(player != null) {
                        player.getModule(CurrencyModule.class).addCurrency(property, eLogMoneyType.EnterNameActivity, eLogMoneyType.EnterNameActivityGoldReward);
                        EnterNameProto.GetEnterNameDisperseGoldRewardRespMsg.Builder clientMsg = EnterNameProto.GetEnterNameDisperseGoldRewardRespMsg.newBuilder();
                        clientMsg.setRet(0);
                        clientMsg.setReward(PropertyHelper.parsePropertyToString(property));
                        player.sendPacket(Protocol.U_ENTER_NAME_DISPERSE_GOLD_GET, clientMsg);
                    }else {
                        EnterNameActivityMgr.getLogger().info("enter name activity {} user {} reward {}", config.getActivityInfo().getActivityId(), packet.getPlayerId(), PropertyHelper.parsePropertyToString(property));
                    }
                }
            }
        }
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
